# -*-coding:utf-8-*-
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer, BadData
from django.conf import settings


def generate_verify_email_url(user_id):
    """
    生成邮箱验证链接
    :param user_id:
    :return: verify_url
    """
    serializer = Serializer(settings.SECRET_KEY, expires_in=3600*2)
    data = serializer.dumps({'user_id':user_id})
    return data.decode()


def check_verify_email_token(token):
    """
    验证token并提取user
    :param token: 用户信息签名后的结果
    :return: user, None
    """
    s = Serializer(settings.SECRET_KEY, expires_in=3600*2)
    try:
        data = s.loads(token)
    except Exception as e:
        return None

    return data.get('user_id')


